package com.mryx.demo.controller;


import com.mryx.demo.pojo.Supplier;
import com.mryx.demo.service.SupplierService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpCookie;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.List;

/**
 * 供应商控制层
 * */
@RestController
@RequestMapping("/supplier")
@CrossOrigin(allowCredentials = "true")
public class SupplierController {

    @Autowired
    SupplierService supplierService;
    @Resource
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 获取供应商信息
     */
    @RequestMapping(value = "/getAllSupplier",method = RequestMethod.GET)
    @ApiOperation(value ="获取供应商信息",notes = "获取供应商信息")
    public List<Supplier>getAllSupplier(){
        return supplierService.getAllSupplier();
    }

    /**
     * 根据供应商编号修改审核状态
     * @param sid
     * @return
     */
    @RequestMapping(value = "/examine",method = RequestMethod.GET)
    @ApiOperation(value ="根据供应商编号修改审核状态",notes = "根据供应商编号修改审核状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "sid",value = "",required = true,dataType = "int"),
                    @ApiImplicitParam(name = "examine",value = "",required = true,dataType = "int")})
    public int examine(int sid,int examine){
        int i=supplierService.examine(sid,examine);
        if(i>0){
            return 0;
        }
        return 1;
    }

    //根据手机号修改密码
    @RequestMapping(value = "/updPasswd",method = RequestMethod.GET)
    @ApiOperation(value ="根据手机号修改密码",notes = "根据手机号修改密码")
    @ApiImplicitParams({@ApiImplicitParam(name = "phone",value = "",required = true,dataType = "String"),
            @ApiImplicitParam(name = "password",value = "",required = true,dataType = "String")})
    public int updPasswd(String phone,String password){
        int i=supplierService.updPasswd(phone,password);
        if(i>0){
            return 0;
        }
        return 1;
    }



    //注册
    @RequestMapping(value = "/register",method = RequestMethod.GET)
    @ApiOperation(value ="注册",notes = "注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "password",value = "",required = true,dataType = "String"),
            @ApiImplicitParam(name = "login",value = "",required = true,dataType = "String")})
    public String register(String login,String password,String phone){
        int i=supplierService.register(login,password,phone);
        if(i>0){
            return "0";
        }
        return "1";
    }

    //登录
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    @ApiOperation(value ="登录",notes = "登录")
    @ApiImplicitParams({@ApiImplicitParam(name = "password",value = "",required = true,dataType = "String"),
                         @ApiImplicitParam(name = "login",value = "",required = true,dataType = "String")})
    public String login(String login, String password, HttpServletResponse response){
        Supplier i=supplierService.login(login,password);
        if(i!=null){
            Integer sid = i.getSid();
            redisTemplate.opsForValue().set("gyslogin",login);
            return "0";
        }
        return "1";
    }

    @PostMapping("/addInformation")
    @ApiOperation(value = "添加供应商信息",notes = "添加供应商信息")
    public String addInformation(@ApiParam(name="name",value="String",required=true)String name,
                                 @ApiParam(name="contacts",value="String",required=true)String contacts,
                                 @ApiParam(name="telephone",value="String",required=true)String telephone,
                                 @ApiParam(name="address",value="String",required=true)String address,
                                 @ApiParam(name="licensez",value="营业照正面",required=true)@RequestParam("licensez") MultipartFile licensez,
                                 @ApiParam(name="licensef",value="营业照反面",required=true)@RequestParam("licensef") MultipartFile licensef,
                                 HttpServletRequest request){
        //获取供应商ID
        String login=redisTemplate.opsForValue().get("gyslogin");
        System.out.println(login);
        Supplier supplier = new Supplier();
        supplier.setName(name);
        supplier.setContacts(contacts);
        supplier.setTelephone(telephone);
        supplier.setAddress(address);

   //    Supplier supplier=new Supplier();
        supplier.setLogin(login);
        if (!licensez.isEmpty()) {
            try {
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(new File("G:\\mryx\\src\\main\\resources\\static\\photo\\"+supplier.getName()+"z.jpg")));//保存图片到目录下
                out.write(licensez.getBytes());
                out.flush();
                out.close();
                String filename="G:\\mryx\\src\\main\\resources\\static\\photo\\"+supplier.getName()+"z.jpg";
                supplier.setLicensez(filename);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return " 上传失败," + e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return " 上传失败," + e.getMessage();
            }
        } else {
            return "上传失败，因为文件是空的.";
        }
        if (!licensef.isEmpty()) {
            try {
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(new File("G:\\mryx\\src\\main\\resources\\static\\photo\\"+supplier.getName()+"f.jpg")));//保存图片到目录下
                out.write(licensef.getBytes());
                out.flush();
                out.close();
                String filename="G:\\mryx\\src\\main\\resources\\static\\photo\\"+supplier.getName()+"f.jpg";
                supplier.setLicensef(filename);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return " 上传失败," + e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return " 上传失败," + e.getMessage();
            }
        } else {
            return "上传失败，因为文件是空的.";
        }
        if(supplierService.InfoInput(supplier) !=0){
            return "0";
        }
        return "1";
    }
}
